package Offer077;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 链表排序
 *
 * @author 23737
 * @time 2021.12.11
 */
public class Test {
    public static void main(String[] args) {

    }
}

class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

/**
 * List集合进行存储
 */
class Solution {
    public ListNode sortList(ListNode head) {
        if(head == null){
            return null;
        }
        ListNode res = head;
        List<Integer> list = new ArrayList<>();
        while(res != null){
            list.add(res.val);
            res = res.next;
        }
        res = head;
        Collections.sort(list);
        for(Integer i : list){
            res.val = i;
            res = res.next;
        }
        return head;
    }
}
